#!/bin/bash

start-test() {
    local ip=$(cat $TEST_DIR/databases/*/metadata/ip)
    local port=$(cat $TEST_DIR/databases/*/metadata/port)
    local pass=$(cat $TEST_DIR/databases/*/metadata/password)
    local image=$(cat $TEST_DIR/databases/*/metadata/image)
    DB_CONNECTION_TEST="docker container run --rm --name connection-test --entrypoint /usr/local/bin/nats docker.io/natsio/nats-box:0.14.1 server check connection --server=nats://$ip:$port" \
    timeout --foreground 1m bash -c "wait-for-db-connection"
    KINE_IMAGE=$IMAGE KINE_ENDPOINT="nats://$ip:$port?noEmbed" provision-kine
    local kine_url=$(cat $TEST_DIR/kine/*/metadata/url)
    K3S_DATASTORE_ENDPOINT=$kine_url provision-cluster
}
export -f start-test

# test minimum supported version and most recent version of NATS
VERSION_LIST="\
    nats 2.9.25
    nats 2.10.12"

while read ENGINE VERSION; do
    LABEL=$ENGINE-$VERSION DB_PASSWORD_ENV=NATS_JS_PASSWORD DB_ARGS="-js" DB_IMAGE=docker.io/library/$ENGINE:$VERSION run-test
done <<< $VERSION_LIST

